package com.tjf.record;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.tjf.mymp3lameen.mp3.Mp3EncodeThread;
import com.tjf.record.xunfei.YjhWebSocketMager;

import org.json.JSONObject;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RecordToTextManager {

    YjhWebSocketMager webSocketMager;
    RecordToTextBean toTextBean;
    public static String filePath;

    public RecordToTextManager(RecordToTextBean toTextBean) {
        this.toTextBean = toTextBean;
    }

    public void onRecordToTextStart(Context context, String lang, MyTextCallBack callBack) {
        webSocketMager = new YjhWebSocketMager();
        Log.e("onRecordToTextStart", "recordToTextStart====" + lang);
        webSocketMager.initWebSocket(toTextBean.getAppId(), toTextBean.getAppServer(),
                lang, toTextBean.getSid(), new MySocketCallBack() {
                    @Override
                    public void onStart() {
                        Log.e("webSocketMager", "webSocketMager   onStart");
                        Plug.startRecording(context, new MyCallBack() {
                            @Override
                            public void onStart(int bufferSize) {

                            }

                            @Override
                            public void onSuccess(short[] shortBuffer, int read) {
                                if (callBack != null) {
                                    callBack.onBuffer(shortBuffer);
                                }
                                webSocketMager.sendBuff(ByteUtils.toBytes(shortBuffer));
                            }

                            @Override
                            public void onComplete() {
                            }
                        });
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        callBack.onSuccess(jsonObject);
                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        Plug.endRecording();
                        callBack.onFail(jsonObject);
                    }

                    @Override
                    public void onComplete() {
                        Plug.endRecording();
                    }
                });

    }

    public void onRecordToTextStartAndPath(Context context, MyTextCallBack callBack) {
        webSocketMager = new YjhWebSocketMager();
        webSocketMager.initWebSocket(toTextBean.getAppId(), toTextBean.getAppServer(),
                new MySocketCallBack() {
                    @Override
                    public void onStart() {
                        Plug.startRecording(context, new MyCallBack() {
                            @Override
                            public void onStart(int bufferSize) {
                                initMp3(bufferSize, callBack);
                            }

                            @Override
                            public void onSuccess(short[] shortBuffer, int read) {

                                webSocketMager.sendBuff(ByteUtils.toBytes(shortBuffer));
                                saveMp3(shortBuffer, read);
                                if (callBack != null) {
                                    Log.e("sendBuff回传  ", "====回传 发送信息");
                                    callBack.onBuffer(shortBuffer);
                                }
                            }

                            @Override
                            public void onComplete() {
                                stopMp3();
                            }
                        });
                    }

                    @Override
                    public void onSuccess(JSONObject jsonObject) {
                        callBack.onSuccess(jsonObject);
                    }

                    @Override
                    public void onFail(JSONObject jsonObject) {
                        Plug.endRecording();
                    }

                    @Override
                    public void onComplete() {
                        Plug.endRecording();
                    }
                });

    }


    public void onRecordToTextEnd() {

        Plug.endRecording();
        stopMp3();
        webSocketMager.close();
    }

    Mp3EncodeThread mp3EncodeThread;

    private void initMp3(int bufferSize, MyTextCallBack callBack) {
        if (TextUtils.isEmpty(toTextBean.getFilePath())) {
            return;
        }
        File dev = new File(toTextBean.getFilePath());
        if (!dev.exists()) {
            dev.mkdirs();
        }
        String mp3Name = "";
        if (TextUtils.isEmpty(toTextBean.getFileName())) {
            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
            String formattedTime = sdf.format(currentDate);
            mp3Name = formattedTime + ".mp3";
        } else {
            mp3Name = toTextBean.getFileName();
        }
        File mp3file = new File(toTextBean.getFilePath(), mp3Name);
        filePath = mp3file.getAbsolutePath();

        mp3EncodeThread = new Mp3EncodeThread(mp3file, bufferSize);
        mp3EncodeThread.start();
    }

    private void saveMp3(short[] buffer, int read) {
        if (TextUtils.isEmpty(toTextBean.getFilePath())) {
            return;
        }
        if (mp3EncodeThread == null) {
            return;
        }
        mp3EncodeThread.addChangeBuffer(new Mp3EncodeThread.ChangeBuffer(buffer, read));
    }

    private void stopMp3() {
        if (toTextBean == null) {
            return;
        }
        if (TextUtils.isEmpty(toTextBean.getFilePath())) {
            return;
        }
        if (mp3EncodeThread == null) {
            return;
        }
        mp3EncodeThread.stopSafe(() -> mp3EncodeThread = null);
    }

    public static String getFilePath() {
        return filePath;
    }
}
